package com.coocaa.ops.plus.dashboard.v1.codegen.validator;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.tool.exception.ExceptionUtil;
import com.coocaa.ops.plus.dashboard.v1.codegen.constant.SqlConstants;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.query.DbTableQueryModel;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author bijiahao
 * @description
 * @date 2020/1/2 17:45
 */
@Slf4j
@UtilityClass
public class DbValidator {

    public static String valid(DbTableQueryModel model) {
        try {
            Class.forName(SqlConstants.DRIVER);
            Connection conn = DriverManager.getConnection(model.getUrl(), model.getUsername(), model.getPassword());
            return R.SUCCESS;
        } catch (ClassNotFoundException e) {
            log.error(ExceptionUtil.getStackTraceAsString(e));
            return e.getMessage();
        } catch (SQLException e) {
            log.error(ExceptionUtil.getStackTraceAsString(e));
            return e.getMessage();
        }
    }
}
